package com.th.therp.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.th.therp.mapper.InventoryMapper;
import com.th.therp.mapper.SaleMapper;
import com.th.therp.pojo.dto.page.InventoryPageQueryDTO;
import com.th.therp.pojo.emtity.Inventory;
import com.th.therp.pojo.emtity.Material;
import com.th.therp.pojo.result.PageResult;
import com.th.therp.pojo.vo.MaterialOptionsVo;
import com.th.therp.service.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Log4j2
@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private StoreService storeService;

    @Autowired
    private MaterialService materialService;

    @Autowired
    private MaterialCategoryService materialCategoryService;

    @Autowired
    private MaterialBrandService materialBrandService;

    @Override
    public PageResult page(InventoryPageQueryDTO inventoryPageQueryDTO) {
        PageHelper.startPage(inventoryPageQueryDTO.getPageNum(), inventoryPageQueryDTO.getPageSize());
        Integer storeId = inventoryPageQueryDTO.getStoreId();
        Integer categoryId = inventoryPageQueryDTO.getCategoryId();
        Integer brandId = inventoryPageQueryDTO.getBrandId();
        String storeName = storeService.findNameById(storeId);
        String materialCategory = materialCategoryService.findCategoryNameById(categoryId);
        String materialBrand = materialBrandService.findBrandNameById(brandId);
        Inventory inventory = Inventory.builder()
                .storeName(storeName)
                .materialCategory(materialCategory)
                .materialBrand(materialBrand)
                .build();
        BeanUtils.copyProperties(inventoryPageQueryDTO,inventory);
        Page<Inventory> page = inventoryMapper.page(inventory);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Inventory inventory) {
        inventoryMapper.add(inventory);
    }

    @Override
    public Integer findQuantityByStoreIdAndBarcode(Integer storeId, String materialBarcode) {
        return inventoryMapper.findQuantityByStoreIdAndBarcode(storeId,materialBarcode);
    }

    @Override
    public void updateQuantityByStoreIdAndBarcode(Inventory inventory) {
        inventoryMapper.updateQuantityByStoreIdAndBarcode(inventory);
    }

    //根据仓库Id返回商品选择列表
    @Override
    public List<MaterialOptionsVo> findMaterialOptionsByStoreId(Integer storeId) {
        List<Inventory> inventoryList = inventoryMapper.findMaterialOptionsByStoreId(storeId);
        List<MaterialOptionsVo> materialVoList = new ArrayList<>();
        //用指定仓库的商品列表的商品编码查询商品
        inventoryList.forEach(inventory -> {
                Material material = materialService.findByBarcodeId(inventory.getMaterialBarcode());
                //根据查到的商品信息构造MaterialOptionsVo
                MaterialOptionsVo materialOptionsVo = MaterialOptionsVo.builder()
                        .id(material.getId())
                        .name(material.getName())
                        .build();
                //将构造好的MaterialOptionsVo添加到materialVoList列表中
                materialVoList.add(materialOptionsVo);
        });
        return materialVoList;
    }

    @Override
    public void deleteByStoreIdAndBarcode(Inventory inventory) {
        inventoryMapper.deleteByStoreIdAndBarcode(inventory);
    }

    @Override
    public Integer findQuantityByBarcode(String barcode) {
        return inventoryMapper.findQuantityByBarcode(barcode);
    }

    @Override
    public Integer findQuantityByStoreId(Integer storeId) {
        return inventoryMapper.findQuantityByStoreId(storeId);
    }

    //根据仓库ID和商品代码修改库存数量
    @Override
    public void updateByStoreIdAndBarcode(Integer storeId, String materialBarcode, Integer quantity) {
        inventoryMapper.updateByStoreIdAndBarcode(storeId,materialBarcode,quantity);
    }
}